//  Main.m
//  SubpixelResample
//
//  Created by David Rudie on 2009 May 9.
//  Copyright 2009 David Rudie. All rights reserved.
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//


#import <Foundation/Foundation.h>
#import <GDFramework/GDFramework.h>

#import "SubpixelResample.h"

int main(int argc, char *argv[]) {
	int returnValue = 0;
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

	if (argc < 2) {
		printf("Usage: %s <imageFile>\n", argv[0]);
		returnValue = -1;
		goto APPLICATION_QUIT;
	}

	SubpixelResample *subpixelResample = [[SubpixelResample alloc] init];

	NSString *imageFile = [NSString stringWithCString:argv[1] encoding:NSUTF8StringEncoding];
	GDImage *image = [[GDImage alloc] initWithFile:imageFile];
	if (image == NO) {
		NSLog(@"Error - image cannot be loaded");
		returnValue = -1;
		goto APPLICATION_QUIT;
	}

	[subpixelResample printConfiguration];

	subpixelResample.imageSize = [image size];
	[subpixelResample cropResolution];

	GDImage *newImage;
	if ([subpixelResample.subpixelResample boolValue] == YES) {
		newImage = [image
					imageSubpixelConstrainedToSize:subpixelResample.croppedSize
					cropped:GDCropCenter
					cropRatio:[subpixelResample cropAspectRatio]];
	} else {
		newImage = [image
					imageConstrainedToSize:subpixelResample.croppedSize
					resampled:NO
					cropped:GDCropCenter];
	}

	NSString *pathToImageFile = [imageFile stringByDeletingLastPathComponent];
	if ([pathToImageFile compare:@""])
		pathToImageFile = [pathToImageFile stringByAppendingString:@"/"];

	NSString *fileName = [imageFile lastPathComponent];

	NSString *outputFile = [NSString stringWithFormat:@"%@%@%@%@", pathToImageFile, [subpixelResample thumbnailPrefix], fileName, @".png"];

	if ([[subpixelResample fontFile] compare:@""]) {
		if ([subpixelResample.subpixelResample boolValue] == YES) {
			if ([subpixelResample.embedFilename boolValue] == YES) {
				GDImage *textImage = [[GDImage alloc] initWithSize:NSMakeSize([newImage width] * 3, 12 * 3)];

				GDColor *colorWhite = [textImage colorForRed:1.0 green:1.0 blue:1.0];
				GDColor *colorBlack	= [textImage colorForRed:0.0 green:0.0 blue:0.0];

				GDFont *font = [GDFont trueTypeFontAtPath:[subpixelResample fontFile] points:24.0];

				[textImage drawRectFill:NSMakeRect(0, 0, [newImage width] * 3, 12 * 3) withColor:colorBlack];

				NSString *filenameText = [fileName stringByDeletingPathExtension];
				NSSize filenameTextSize = [font sizeOfString:filenameText];

				if (filenameTextSize.height > 30.0)
					filenameTextSize.height -= filenameTextSize.height - 30.0;
				if (filenameTextSize.height < 30.0)
					filenameTextSize.height += 30.0 - filenameTextSize.height;

				[textImage drawString:filenameText
								point:NSMakePoint(([textImage width] - filenameTextSize.width) / 2, [textImage height] - filenameTextSize.height)
								 font:font color:colorWhite];

				textImage = [textImage imageSubpixelConstrainedToSize:NSMakeSize([newImage width], 12)
															  cropped:GDCropCenter
															cropRatio:[NSNumber numberWithDouble:0.0]];

				[newImage mergeImage:textImage textPosition:0 sourceYPoint:0 width:[newImage width]];
			}

			if ([subpixelResample.embedFilesize boolValue] == YES) {
				GDImage *textImage = [[GDImage alloc] initWithSize:NSMakeSize([newImage width] * 3, 12 * 3)];

				GDColor *colorWhite = [textImage colorForRed:1.0 green:1.0 blue:1.0];
				GDColor *colorBlack	= [textImage colorForRed:0.0 green:0.0 blue:0.0];

				GDFont *font = [GDFont trueTypeFontAtPath:[subpixelResample fontFile] points:24.0];

				[textImage drawRectFill:NSMakeRect(0, 0, [newImage width] * 3, 12 * 3) withColor:colorBlack];

				NSFileManager *fileManager = [NSFileManager defaultManager];
				NSDictionary *fileAttributes = [fileManager fileAttributesAtPath:imageFile traverseLink:YES];
				NSNumber *fileSize;
				fileSize = [fileAttributes objectForKey:NSFileSize];

				NSString *filesizeText = [NSString stringWithFormat:@"%gx%g %@",
										  [image width],
										  [image height],
										  [subpixelResample stringFromFileSize:[fileSize unsignedLongLongValue]]];				
				NSSize filesizeTextSize = [font sizeOfString:filesizeText];

				if (filesizeTextSize.height > 30.0)
					filesizeTextSize.height -= filesizeTextSize.height - 30.0;
				if (filesizeTextSize.height < 30.0)
					filesizeTextSize.height += 30.0 - filesizeTextSize.height;

				[textImage drawString:filesizeText
								point:NSMakePoint(([textImage width] - filesizeTextSize.width) / 2, [textImage height] - filesizeTextSize.height)
								 font:font color:colorWhite];

				textImage = [textImage imageSubpixelConstrainedToSize:NSMakeSize([newImage width], 12)
															  cropped:GDCropCenter
															cropRatio:[NSNumber numberWithDouble:0.0]];

				[newImage mergeImage:textImage textPosition:([newImage height] - 12) sourceYPoint:0 width:[newImage width]];
			}
		} else {
			GDColor *colorWhite = [newImage colorForRed:1.0 green:1.0 blue:1.0 alpha:0.85];
			GDColor *colorBlack = [newImage colorForRed:0.0 green:0.0 blue:0.0 alpha:0.65];

			GDFont *font = [GDFont trueTypeFontAtPath:[subpixelResample fontFile] points:8.0];

			if ([subpixelResample.embedFilename boolValue] == YES) {
				[newImage drawRectFill:NSMakeRect(0, [newImage height] - 11, [newImage width], 12) withColor:colorBlack];

				NSString *filenameText = [fileName stringByDeletingPathExtension];
				NSSize filenameTextSize = [font sizeOfString:filenameText];

				if (filenameTextSize.height == 11.0)
					filenameTextSize.height -= 1.0;
				if (filenameTextSize.height == 12.0)
					filenameTextSize.height -= 2.0;
				[newImage drawString:filenameText
							   point:NSMakePoint(([newImage width] - filenameTextSize.width) / 2, [newImage height] - filenameTextSize.height)
								font:font color:colorWhite];
			}

			if ([subpixelResample.embedFilesize boolValue] == YES) {
				[newImage drawRectFill:NSMakeRect(0, 0, [newImage width], 12) withColor:colorBlack];

				NSFileManager *fileManager = [NSFileManager defaultManager];
				NSDictionary *fileAttributes = [fileManager fileAttributesAtPath:imageFile traverseLink:YES];
				NSNumber *fileSize;
				fileSize = [fileAttributes objectForKey:NSFileSize];

				NSString *filesizeText = [NSString stringWithFormat:@"%gx%g %@",
										  [image width],
										  [image height],
										  [subpixelResample stringFromFileSize:[fileSize unsignedLongLongValue]]];
				NSSize filesizeTextSize = [font sizeOfString:filesizeText];

				[newImage drawString:filesizeText
							   point:NSMakePoint(([newImage width] - filesizeTextSize.width) / 2, filesizeTextSize.height - 8)
								font:font color:colorWhite];
			}
		}
	}

	if (![newImage writeToFile:outputFile type:GDImageTypePNG])
		NSLog(@"Error writing file");

	[image release];
	[SubpixelResample release];

APPLICATION_QUIT:
	[pool release];
	return returnValue;
}
